package com.suixun.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.authority.utils.CommonUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.suixun.dao.userquestionmanage.ReplyMessageDAO;
import com.suixun.dao.userquestionmanage.UserQuestionManageDAO;
import com.suixun.entity.ReplyMessageDO;
import com.suixun.entity.SendMessageInfo;
import com.suixun.entity.UserQuestionDO;
import com.suixun.entity.page.PageModel;
import com.suixun.service.UserQuestionService;
import com.suixun.service.cache.CacheService;
import com.suixun.util.StringUtil;
import com.suixun.vo.ReplyMessageVO;
import com.suixun.vo.UserQuestionVO;
/**
 * 
 * <p>Title: FeedbackServiceImpl</p>
 * <p> 功能描述:反馈信息业务逻辑操作 </p>
 * <p>Company: www.17find.com</p> 
 * @author  陈显强 
 * @date	2016年12月14日下午11:16:27
 * @version 1.0
 */
@Service("userQuestionService")
public class UserQuestionServiceImpl implements UserQuestionService {
	@Resource
	private UserQuestionManageDAO userQuestionManageDAO;
	@Resource
	private ReplyMessageDAO replyMessageDAO;
	@Resource
	private CacheService cacheService;
	/**
	 * 分页查询用户反馈
	 */
	@Override
	public PageModel<UserQuestionVO> queryQuestionList(UserQuestionVO question) throws Exception {
		PageModel<UserQuestionVO> pageModel = new PageModel<UserQuestionVO>();
		if (StringUtil.isNotEmpty(question.getPeopletel())) {
			pageModel.getQueryparam().put("peopletel", new StringBuilder().append("%")
					.append(question.getPeopletel()).append("%").toString());
		}
		if (StringUtil.isNotEmpty(question.getContent())) {
			pageModel.getQueryparam().put("content", new StringBuilder().append("%")
					.append(question.getContent()).append("%").toString());
		}
		if (question.getPagecount() != null) {
			pageModel.setPagesize(question.getPagecount());
		}
		Integer totleRows = userQuestionManageDAO.countRows(pageModel);
		pageModel.setTotlepage(totleRows);
		if (question.getPagenumber() != null) {
			pageModel.setCurrentpage(question.getPagenumber());
		}
		List<UserQuestionDO> userQuestionList = userQuestionManageDAO.queryUserQuestionByPaging(pageModel);
		List<UserQuestionVO> userQuestionVOList = CommonUtils.dbToVo2(userQuestionList, UserQuestionVO.class);
		pageModel.setQueryresult(userQuestionVOList);
		pageModel.getQueryparam().put("status", 1);
		pageModel.getQueryparam().put("content",null);
		Integer warningrows = userQuestionManageDAO.countRows(pageModel);
		pageModel.setWarningrows(warningrows);
		return pageModel;
	}
	
	
	/**
	 * 电话处理
	 */
	@Override
	public Integer resolveByTel(Integer questionid) {
		Map<String,Object> map = new HashMap<>();
		map.put("id", questionid);
		map.put("status", 3);
		return userQuestionManageDAO.updateUserQuestion(map);
	}
	/**
	 * <p>Title: getReplyMessageList</p>
	 * <p> 功能描述:查询回复信息 </p>
	 * @return
	 * @throws Exception 
	 */
	@Override
	public List<ReplyMessageVO> getReplyMessageList(Integer questionid) throws Exception {
		Map<String,Object> map = new HashMap<>();
		map.put("questionid", questionid);
		List<ReplyMessageDO> list = replyMessageDAO.queryReplyMessageList(map);
		return CommonUtils.dbToVo2(list, ReplyMessageVO.class);
	}
	/**
	 * <p>Title: replyMessage</p>
	 * <p> 功能描述:回复 </p>
	 * @param replyMessage
	 * @return
	 */
	@Override
	@Transactional
	public Integer replyMessage(ReplyMessageVO replyMessage,long operationerid) {
		Map<String,Object> map = new HashMap<>();
		map.put("questionid", replyMessage.getQuestionid());
		map.put("content", replyMessage.getContent());
		map.put("operationerid", operationerid);
		Integer isAdd = replyMessageDAO.addReplyMessage(map);
		if (isAdd == 1) {
			map.put("id", replyMessage.getQuestionid());
			map.put("content", null);
			map.put("questionid", null);
			map.put("operationerid", null);
			map.put("status", 2);
			userQuestionManageDAO.updateUserQuestion(map);
			List<UserQuestionDO> userQuestionList = userQuestionManageDAO.queryUserQuestion(map);
			UserQuestionDO userQuestion = null;
			if (userQuestionList.size() > 0) {
				userQuestion = userQuestionList.get(0);
			}
			if (userQuestion != null) {
				//刷新反馈缓存
				cacheService.flushUserQuestion(replyMessage.getQuestionid(), userQuestion.getPeopleid());
			}
		}
		return isAdd;
	}
	/**
	 * <p>Title: deleteReplyMessage</p>
	 * <p> 功能描述:删除管理员回复信息 </p>
	 * @param replyId
	 * @return
	 */
	@Override
	@Transactional
	public Integer deleteReplyMessage(Integer replyId) {
		Map<String,Object> map = new HashMap<>();
		map.put("id", replyId);
		List<ReplyMessageDO> replyList = replyMessageDAO.queryReplyMessageList(map);
		if (replyList.size() == 0) {
			return 0;
		}
		Integer questionid = replyList.get(0).getQuestionid();
		Integer isDelete = replyMessageDAO.deleteReplyMessage(map);
		if (isDelete == 1) {
			map.put("id", null);
			map.put("questionid", questionid);
			map.put("replytype", 1);
			replyList = replyMessageDAO.queryReplyMessageList(map);
			if (replyList.size() == 1) {
				map.put("id", questionid);
				map.put("questionid", null);
				map.put("status", 1);
				userQuestionManageDAO.updateUserQuestion(map);
			}
		}
		return isDelete;
	}
	/**
	 * <p>Title: deleteUserQuestion</p>
	 * <p> 功能描述:删除反馈信息 </p>
	 * @param questionid
	 * @return
	 */
	@Override
	public Integer deleteUserQuestion(Integer questionid,long operationerid) {
		Map<String,Object> map = new HashMap<>();
		map.put("id", questionid);
		map.put("operationerid", operationerid);
		return userQuestionManageDAO.deleteUserQuestion(map);
	}

	/**
	 * <p>Title: queryQuestion</p>
	 * <p> 功能描述:查询指定反馈信息 </p>
	 * @param id
	 * @return
	 * @throws Exception 
	 */
	@Override
	public UserQuestionVO queryQuestion(UserQuestionVO question) throws Exception {
		Map<String,Object> map = new HashMap<>();
		map.put("id", question.getId());
		List<UserQuestionDO> userquestionList = userQuestionManageDAO.queryUserQuestion(map);
		UserQuestionDO userquestion = null;
		if (userquestionList.size() > 0) {
			userquestion = userquestionList.get(0);
		}
		UserQuestionVO userquestionVO = null;
		if (userquestion != null) {
			userquestionVO = CommonUtils.dbToVo2(userquestion, UserQuestionVO.class);
			if (userquestionVO.getClientparam() != null) {
				ObjectMapper mapper = new ObjectMapper();
				mapper.configure(
						com.fasterxml.jackson.core.JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES,
						true);
				UserQuestionVO userquestionTemp = mapper.readValue(userquestionVO.getClientparam(), UserQuestionVO.class);
				userquestionVO.setAppversion(userquestionTemp.getAppversion());
				userquestionVO.setPhonemodel(userquestionTemp.getPhonemodel());
				userquestionVO.setOsversion(userquestionTemp.getOsversion());
			}
		}
		return userquestionVO;
	}
	
}
